package com.zrc.superframe_base.utils.transformations;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.TypedValue;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import java.security.MessageDigest;

/**
 * 类说明：圆形bitmap，外围增加圆环
 * <p>
 * 作  者：小蜗（Richard） on 2017/6/2
 */
public class RingCircleTransformation extends BitmapTransformation {

    private final int RING_COLOR = 0xFFFFFFFF;
    private final float RING_WIDTH = 1;

    private BitmapPool mBitmapPool;
    private Context mContext;

    private float mRingWidth = RING_WIDTH;
    private int mRingColor = RING_COLOR;

    public RingCircleTransformation(Context context) {
        this(Glide.get(context)
                .getBitmapPool());
        this.mContext = context;
    }

    public RingCircleTransformation(Context context, int ringWidth) {
        this(Glide.get(context)
                .getBitmapPool());
        mContext = context;
        mRingWidth = ringWidth;
    }

    public RingCircleTransformation(Context mContext, int mRingWidth, @ColorRes int ringColor) {
        this(Glide.get(mContext)
                .getBitmapPool());
        this.mContext = mContext;
        this.mRingWidth = mRingWidth;
        this.mRingColor = mContext.getResources()
                .getColor(ringColor);
    }

    public RingCircleTransformation(BitmapPool pool) {
        this.mBitmapPool = pool;
    }


    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        this.mBitmapPool = pool;
        Bitmap source = toTransform;
        int size = Math.min(source.getWidth(), source.getHeight());

        int width = (source.getWidth() - size) / 2;
        int height = (source.getHeight() - size) / 2;

        Bitmap bitmap = mBitmapPool.get(size, size, Bitmap.Config.ARGB_8888);
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        if (width != 0 || height != 0) {
            // 容器本身是矩形，移动需要显示的View
            Matrix matrix = new Matrix();
            matrix.setTranslate(-width, -height);
            shader.setLocalMatrix(matrix);
        }
        paint.setShader(shader);
        paint.setAntiAlias(true);

        float r = size / 2f;
        canvas.drawCircle(r + dp2px(mRingWidth), r + dp2px(mRingWidth), r - dp2px(mRingWidth * 2), paint);

        // 绘制圆环，啦啦啦
        paint = new Paint();
        paint.setStrokeWidth(dp2px(mRingWidth));
        paint.setColor(mRingColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        canvas.drawCircle(r + dp2px(mRingWidth), r + dp2px(mRingWidth), r - dp2px(mRingWidth * 2), paint);
        return bitmap;
    }

    protected float dp2px(float dpVal) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, mContext.getResources()
                .getDisplayMetrics());
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {
        messageDigest.digest(getClass().getName().getBytes());
    }
}
